package com.lfnyxy.common;

import java.io.*;

/**
 * Created by ted on 17/5/21.
 */
public class FileTool {

    /**
     * 获取项目的根目录
     * @return
     */
    public static String GetRootPath(){
        File directory = new File("");// 参数为空
        try {
            return directory.getCanonicalPath();
        } catch (IOException e) {

        }
        return StringTool.Empty();
    }

    /**
     * 获取文件内容
     * @param filePath
     * @return
     */
    public static String ReadFileContent(String filePath){
    InputStreamReader inputReader = null;
    BufferedReader bufferedReader = null;
    StringBuilder sb = new StringBuilder();

		try {
        InputStream inputStream = new FileInputStream(new File(filePath));
        inputReader = new InputStreamReader(inputStream, "UTF-8");
        bufferedReader = new BufferedReader(inputReader);

        String line = null;
        while ((line = bufferedReader.readLine()) != null) {
            sb.append(line);
        }

    } catch (Exception e) {
        System.out.println(e.getMessage());
    }finally{
        if(bufferedReader != null || inputReader != null){
            try {
                bufferedReader.close();
                inputReader.close();
            } catch (IOException e) {
                System.out.println(e.getMessage());
            }
        }

    }

		return sb.toString();
}


    public static void WriteToFile(String filePath, String content){
        WriteToFile(filePath, content, false);
    }

    /**
     * 写文件内容
     * @param filePath
     * @param content
     * @param append
     */
    public static void WriteToFile(String filePath, String content, boolean append){
        File file = new File(filePath);
        if(!file.exists()){
            File folder = new File(file.getParent());
            if(!folder.exists()){
                folder.mkdirs();
            }
        }
		/*if(!file.getParentFile().exists()){
			file.mkdirs();
		}*/

        try {
            file.createNewFile();
        } catch (IOException e) {
            System.out.println(e.getMessage());
        }

        OutputStreamWriter fWrite = null;
        BufferedWriter bWriter = null;
        try {
            //fWriter = new FileWriter(file, false);

            fWrite = new OutputStreamWriter(new FileOutputStream(file,append),"UTF-8");

            bWriter = new BufferedWriter(fWrite);
            bWriter.write(content);
            bWriter.flush();
        } catch (IOException e) {
            System.out.println(e.getMessage());
        }finally{
            if(bWriter != null  || fWrite != null){
                try {
                    bWriter.close();
                    fWrite.close();
                } catch (IOException e) {
                    System.out.println(e.getMessage());
                }
            }
        }
    }


    /**
     * 按照指定的路径和编码格式保存文件内容，这个方法因为用到了字符串作为载体，为了正确写入文件（不乱码），只能写入文本内容，安全方法
     *
     * @param data
     *     将要写入到文件中的字节数据
     * @param path
     *     文件路径,包含文件名
     * @return boolean
     *      当写入完毕时返回true;
     */
    public static boolean writeFile(byte data[], String path , String code){
        boolean flag = true;
        OutputStreamWriter osw = null;
        try{
            File file = new File(path);
            if(!file.exists()){
                file = new File(file.getParent());
                if(!file.exists()){
                    file.mkdirs();
                }
            }
            if("asci".equals(code)){
                code = "UTF-8";
            }
            osw = new OutputStreamWriter(new FileOutputStream(path),code);
            osw.write(new String(data,code));
            osw.flush();
        }catch(Exception e){
            e.printStackTrace();
            flag = false;
        }finally{
            try{
                if(osw != null){
                    osw.close();
                }
            }catch(IOException e){
                e.printStackTrace();
                flag = false;
            }
        }
        return flag;
    }
}
